package com.server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.*;

public class ClaimPointServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
		
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		String msg = null;
		String username = req.getParameter("username");
		String resID = req.getParameter("r1");
		int numRes;
		int numPointToClaim;

		ConnectDB();
		
		try 
		{
			if (resID == null ||resID.equals("null"))
			{
				CustomerInfo customerInfo = CustomerHelper.GetSelectedCustomer(username);
				int currentPoint = customerInfo.point;
				msg = "Invalid Selection";
				req.setAttribute("username", username);
				req.setAttribute("msg", msg);
				req.setAttribute("point", currentPoint);
				req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);
			}
			else
			{
				CustomerHelper.Setup(connection);
				ReservationHelper.Setup(connection, username);
				FlightHelper.Setup(connection);
				
				ReservationInfo resInfo = ReservationHelper.GetSelectedReservation(Integer.parseInt(resID));
				FlightInfo flightInfo = FlightHelper.GetSelectedFlights(resInfo.flightID);
				
				int pointToAdd = (int)flightInfo.distance;
					
				CustomerHelper.AddPoint(username, pointToAdd); 
				msg = pointToAdd + " points have been added to " + username;
				ReservationHelper.SetInactive(Integer.parseInt(resID));
				
				CustomerInfo customerInfo = CustomerHelper.GetSelectedCustomer(username);
				int currentPoint = customerInfo.point;
				
				numRes = ReservationHelper.GetReservationList().size();
				numPointToClaim = ReservationHelper.GetNumPointsToClaim();
				req.setAttribute("numRes", numRes);
				req.setAttribute("numPointToClaim", numPointToClaim);
				
				req.setAttribute("username", username);
				req.setAttribute("msg", msg);
				req.setAttribute("point", currentPoint);
				req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);
			}
			
		} 
		catch (SQLException e1) 
		{
			e1.printStackTrace();
		}
				
		
	}

}

